package com.cmcc.linlaker.ui.mine.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cmcc.linlaker.R;
import com.cmcc.linlaker.ui.mine.MessageActivity;
import com.cmcc.linlaker.ui.mine.MessageDetailActivity;
import com.cmcc.linlaker.ui.mine.bean.BookBean;
import com.cmcc.linlaker.ui.mine.bean.MessageBean;

import java.util.List;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {

    private List<MessageBean.MessageListBean> itemList;
    private Context mContext;

    public MessageAdapter(Context mContext, List<MessageBean.MessageListBean> itemList) {
        this.itemList = itemList;
        this.mContext = mContext;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.message_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        MessageBean.MessageListBean item = itemList.get(position);

        if (item.sysMsgInner != null) {
            holder.text_name.setText(item.sysMsgInner.msgTitle);
            holder.text_date.setText(item.sysMsgInner.sendDate);

            if (item.sysMsgInner.sysMsgInnerRecord.readStatus.equals("1")) {
                //已读
                holder.img_read_icon.setVisibility(View.GONE);
            } else {
                holder.img_read_icon.setVisibility(View.VISIBLE);
            }
        } else {
            holder.text_name.setText("无标题");
            holder.text_date.setText("暂无内容");

            holder.img_read_icon.setVisibility(View.GONE);

        }

    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView text_name, text_date;
        public ImageView img_read_icon;

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);

            text_name = itemView.findViewById(R.id.text_name);
            text_date = itemView.findViewById(R.id.text_date);
            img_read_icon = itemView.findViewById(R.id.img_read_icon);
        }

        @Override
        public void onClick(View v) {
            if (itemList.get(getAdapterPosition()).sysMsgInner != null) {
                Intent intent = new Intent(mContext, MessageDetailActivity.class);
                intent.putExtra("msgBean", itemList.get(getAdapterPosition()));
                mContext.startActivity(intent);
            }
        }
    }

}

